package cn.edu.zhku.controller;

import cn.edu.zhku.aspect.annotation.*;
import cn.edu.zhku.aspect.enmu.DataType;
import cn.edu.zhku.constant.ResponseCode;
import cn.edu.zhku.dto.FormDto;
import cn.edu.zhku.dto.UserDto;
import cn.edu.zhku.pojo.Task;
import cn.edu.zhku.pojo.form.*;
import cn.edu.zhku.service.*;
import cn.edu.zhku.swagger.notes.FormApiNotes;
import cn.edu.zhku.util.ResultUtil;
import cn.edu.zhku.service.State;
import cn.edu.zhku.util.ShiroUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/form")
public class FormController {

    @Autowired
    private FormService formService;
    @Autowired
    private TaskService taskService;

    @Autowired
    @Qualifier("notReadState")
    private State notReadState;

    @Autowired
    @Qualifier("readState")
    private State readState;

    @Autowired
    @Qualifier("redoState")
    private State redoState;

    @Autowired
    @Qualifier("saveState")
    private State saveState;

    @RequiresPermissions("form:get")
    @ApiOperation("获取表单的详细信息")
    @CheckDataExist("没有查到表单相关信息")
    @GetMapping("/{formId}")
    public JSONObject getForm(@PathVariable Integer formId) {
        Form form = this.formService.findById(formId);
        return ResultUtil.getResult(ResponseCode.ok, "成功返回表单的详细信息", form);
    }

    @RequiresRoles("teacher")
    @RequiresPermissions("form:list")
    @Paging(DataType.FORM)
    @CheckDataExist("没有查到相关表单信息")
    @PostMapping("/tea")
    @ApiOperation(value = "教师端获取某任务下某群组中已提交的表单列表", notes = FormApiNotes.tea)
    public JSONObject tea(@RequestBody UserDto userDto) {
        PageHelper.startPage(userDto.getPageNum(), userDto.getPageSize());
        List<Form> forms = this.formService.findFormTea(userDto);
        PageInfo<Form> pageInfo = new PageInfo<>(forms);
        return ResultUtil.getResult(ResponseCode.ok, "成功返回表单列表",pageInfo);
    }

    @RequiresRoles("student")
    @RequiresPermissions("form:submit")
    @CheckGroupUserExist("您已被移除出群组")
    @CheckGroupTaskExist("任务已被移除出群组")
    @SetUserId
    @PostMapping("/submit")
    @ApiOperation(value = "学生端提交表单", notes = FormApiNotes.submitForm)
    public JSONObject submitFormStu(@RequestBody FormDto formDto) {
        Task task = this.taskService.findById(formDto.getTaskId());
        if (task.getExpireTime().before(new Date()))
            return ResultUtil.getResult(ResponseCode.conflict, "任务已过期");
        notReadState.doAction(formDto);
        return ResultUtil.getResult(ResponseCode.ok, "提交表单成功");
    }

    @RequiresRoles("student")
    @RequiresPermissions("form:save")
    @CheckGroupUserExist("您已被移除出群组")
    @CheckGroupTaskExist("任务已被移除出群组")
    @SetUserId
    @PatchMapping("/save")
    @ApiOperation(value = "学生端暂时保存表单", notes = FormApiNotes.saveForm)
    public JSONObject saveFormStu(@RequestBody FormDto formDto) {
        Task task = this.taskService.findById(formDto.getTaskId());
        if (task.getExpireTime().before(new Date()))
            return ResultUtil.getResult(ResponseCode.conflict, "任务已过期");
        saveState.doAction(formDto);
        return ResultUtil.getResult(ResponseCode.ok, "保存表单成功");
    }

    @RequiresRoles("teacher")
    @CheckFormExist("表单不存在")
    @PatchMapping("/read")
    @ApiOperation(value = "教师端查阅表单", notes = FormApiNotes.readForm)
    public JSONObject readFormTea(@RequestBody FormDto formDto) {
        readState.doAction(formDto);
        return ResultUtil.getResult(ResponseCode.ok, "修改表单状态为已阅成功");
    }

    @RequiresRoles("teacher")
    @CheckFormExist("表单不存在")
    @PatchMapping("/redo")
    @ApiOperation(value = "教师端将表单谴回重做", notes = FormApiNotes.redoForm)
    public JSONObject redoFormTea(@RequestBody FormDto formDto) {
        redoState.doAction(formDto);
        return ResultUtil.getResult(ResponseCode.ok, "修改表单状态为重做成功");
    }

}
